<?php
class FileAction extends Action{
	public function _initialize()
	{
		$this->assign('root',C("WWW_PATH")."codeAdmin/");
		$this->assign('cm',C("WWW_PATH")."CodeMirror-master/");
	}
	/**
	 * 编辑器
	 */
	public function get()
	{
		$this->display();
	}
	/**
	 * 获取代码
	 */
	public function code()
	{
		$file=C('DIR').$_GET["file"];
		$ctt=file_get_contents(iconv('UTF-8', 'GB2312', $file));
		if (preg_match('/\.php/', $file)&&preg_match('/<\?/', $ctt)) {
			$type='application/x-httpd-php';
		}else{
			$type='text/html';
		}
		$this->ajaxReturn(array(
			"type"=>$type,
			"content"=>$ctt
			));
	}
	/**
	 * 保存文件
	 */
	public function save()
	{
		$file=$_POST['file'];$file=C('DIR').$file;
		$content=$_POST['content'];
		$result=file_put_contents(iconv('UTF-8', 'GB2312', $file), $content);
		if ($result){
			$this->ajaxReturn(array(
					'result'=>true,
					'msg'=>'写入成功，'.$result.'字符',
			));
		}else {
			$this->ajaxReturn(array(
					'result'=>false,
					'msg'=>'写入0个字符',
			));
		}
		
	}
	/**
	 * 新建文件
	 */
	public function newfile()
	{
		$currentpath=C('DIR').$_POST['currentpath'];
		$currentpath=iconv('UTF-8', 'GB2312', $currentpath);
		$parentfile=C('DIR').$_POST["file"];//父目录下创建item
		$parentfile=iconv('UTF-8', 'GB2312', $parentfile);//TODO
		$filename=$_POST['filename'];
		$filename=iconv('UTF-8', 'GB2312', $filename);
		
		$file=$currentpath.$filename;
		if (is_dir($currentpath)&&!is_file($file)) {//如果当前路径是文件夹，并且指定文件不存在
			$handle=fopen($file, "w");//创建文件
			if ($handle) {
				$result=array(
						'result'=>true,
						'msg'=>'文件创建成功'
				);
			}else {
				$result=array(
						'result'=>false,
						'msg'=>'文件创建失败'
				);
			}
			$this->ajaxReturn($result);
		}else {//否则,不是文件夹
			if(is_file($parentfile)){//文件存在于父目录或当前目录
				$this->ajaxReturn(array(
					'result'=>false,
					'msg'=>'文件已经存在'
				));
			}else {
				$handle=fopen($parentfile, "w");//create
				if ($handle) {
					$result=array(
							'result'=>true,
							'msg'=>'文件创建成功'
					);
				}else {
					$result=array(
							'result'=>false,
							'msg'=>'文件创建失败'
					);
				}
				$this->ajaxReturn($result);
			};
		}
	}
	/**
	 * 新建文件夹
	 */
	public function newdir() {
		$currentpath=C('DIR').$_POST['currentpath'];
		$currentpath=iconv('UTF-8', 'GB2312', $currentpath);
		$parentpath=C('DIR').$_POST["parentpath"];//父目录下创建item
		$parentpath=iconv('UTF-8', 'GB2312', $parentpath);//TODO
		$filename=$_POST['filename'];
		$filename=iconv('UTF-8', 'GB2312', $filename);
		
		$path=$currentpath.'/'.$filename;
		if (is_dir($currentpath)) {//如果当前路径是文件夹,并且指定目录不存在
			if (is_dir($path)) {//要创建的目录存在
				$result=array(
						'result'=>false,
						'msg'=>'文件夹存在'
				);
			}else {//不存在则创建
				if (@mkdir($path)) {
					$result=array(
							'result'=>true,
							'msg'=>'文件夹创建成功'
					);
				}else {
					$result=array(
							'result'=>false,
							'msg'=>'文件夹创建失败'
					);
				}
			}
			$this->ajaxReturn($result);
		}else {//否则当前路径不是文件夹
			if (!is_dir($parentpath.'/'.$filename)) {//父目录的item不存在
				if (@mkdir($parentpath.'/'.$filename)) {//创建
					$result=array(
							'result'=>true,
							'msg'=>'文件夹创建成功'
					);
				}else {
					$result=array(
							'result'=>false,
							'msg'=>'文件夹创建失败'
					);
				}
			}else {
				$result=array(
						'result'=>false,
						'msg'=>'文件夹存在'
				);
			}
			$this->ajaxReturn($result);
		}
	}
	/**
	 * 删除文件、目录
	 */
	public function delete()
	{
		$currentpath=$_POST['currentpath'];
		$currentpath=iconv('UTF-8', 'GB2312', $currentpath);
		$path=C('DIR').$_POST["file"];
		$path=iconv('UTF-8', 'GB2312', $path);
		
		if((bool)opendir($path)){
			if (@rrmdir($path)) {
				$result=array(
						'result'=>true,
						'msg'=>'删除文件夹成功'
				);
			}else {
				$result=array(
						'result'=>false,
						'msg'=>'删除文件夹失败'
				);
			}
		}else if((bool)fopen($path, "r")){
			if (@unlink($path)) {
				$result=array(
					'result'=>true,
					'msg'=>'删除文件成功'	
				);
			}else {
				$result=array(
						'result'=>false,
						'msg'=>'删除文件失败'
				);
			}
			
		}else {
			$result=array(
					'result'=>false,
					'msg'=>'删除失败'
			);
		}
		$this->ajaxReturn($result);
	}
	/**
	 * 重命名
	 */
	public function rename() {
		$oldtpath=C('DIR').$_POST['oldpath'];
		$oldtpath=iconv('UTF-8', 'GB2312', $oldtpath);
		$newname=$_POST["newname"];
		$newname=iconv('UTF-8', 'GB2312', $newname);
		$newpath=preg_replace('/\/[\d\D][^\/]*$/', '/'.$newname, $oldtpath);
		if (is_file($oldtpath)) {//是文件
			if (is_file($newpath)) {//文件存在，失败
				$result=array(
						'result'=>false,
						'msg'=>'文件已存在，重命名失败'
				);
			}else {//文件不存在，开始重命名
				$result=rename($oldtpath, $newpath);
				if ((bool)$result) {
					$result=array(
							'result'=>true,
							'msg'=>'重命名成功'
					);
				}else {
					$result=array(
							'result'=>false,
							'msg'=>'重命名失败'
					);
				}
			}
			$this->ajaxReturn($result);
		}elseif (is_dir($oldtpath)){//是目录
			if (is_dir($newpath)) {//目录存在
				$result=array(
						'result'=>false,
						'msg'=>'目录已存在，重命名失败'
				);
			}else {//目录不存在
				$result=rename($oldtpath, $newpath);
				if ((bool)$result) {
					$result=array(
							'result'=>true,
							'msg'=>'重命名成功'
					);
				}else {
					$result=array(
							'result'=>false,
							'msg'=>'重命名失败'
					);
				}
			};
			$this->ajaxReturn($result);
		}else {//错误
			$this->ajaxReturn(array(
					'result'=>false,
					'msg'=>'重命名失败'
			));
		}
		
	}
}